package com.SouthernWall_404.AgeRight.Common.Event;

import com.SouthernWall_404.AgeRight.Common.API.LockAPI.LockAPI;
import com.SouthernWall_404.AgeRight.Common.API.LockAPI.LockableBlocksRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class onLeftClick {
    @SubscribeEvent
    public static void onLeftClick(BlockEvent.BreakEvent event)
    {
        BlockState block =event.getState();
        BlockPos blockPos=event.getPos();
        Player player=event.getPlayer();
        Level level=player.level();
        LevelChunk levelChunk=level.getChunkAt(blockPos);
        if(LockableBlocksRegistry.isLockable(block))//若为可锁方块
        {
            if(LockAPI.hasLock(blockPos,level))//上锁了
            {
                if(player.isCreative()&&player.hasPermissions(4))//如果是创造模式且具有足够权限
                {
                    LockAPI.removeLock(blockPos,level);//移除此锁
                    return;//一切正常
                }
                else//如果不是是创造模式或不具有足够权限
                {
                    event.setCanceled(true);
                    event.setResult(Event.Result.DENY);//取消
                }
            }
        }
    }
}
